using System;
using System.Collections.Generic;
using System.Drawing;
using AtDevice;
//using Common;

namespace GtkControls
{
    /// <summary>
    /// Represents an item of data to display.
    /// </summary>
    public class DataRow
    {
        /// <summary>
        /// Get or set label for this data row.
        /// </summary>
        public string Label;

        /// <summary>
        /// Get or set the data to display.
        /// </summary>
        public List<double> Data = new List<double>();

        /// <summary>
        /// The pen used to draw this row of data.
        /// </summary>
        public Cairo.Color Pen;

        /// <summary>
        /// Construct new data row with label and pen.
        /// </summary>
        public DataRow(string label, Cairo.Color pen)
        {
            Label = label;
            Pen = pen;
        }
    }
    
    public static class GdkExtension
    {
        public static Gdk.Size GetSize(this Gdk.Window window)
        {
            int width = 0, height = 0;
            window.GetSize(out width, out height);
            return new Gdk.Size(width, height);    
        }
    }

    [System.ComponentModel.Category("GtkControls")]
    [System.ComponentModel.ToolboxItem(true)]
    public partial class StatisticsWidget : Gtk.Bin
    {
        private CairoDrawer _drawer = new CairoDrawer();
        List<DataRow> _data = new List<DataRow>();
        List<KeyValuePair<string, Cairo.Color>> _labels = new List<KeyValuePair<string, Cairo.Color>>();
        
        public StatisticsWidget()
        {
            this.Build();
            DoSetup();
            drawingarea.ExposeEvent += OnStatisticsAreaExposeEvent;
        }

        private void DoSetup()
        {
            _data.Add(new DataRow("Download", new Cairo.Color (0.2, 1.0, 0.2, 0.7)));
            _data.Add(new DataRow("Upload", new Cairo.Color (1.0, 0.2, 0.2, 0.7)));
            _labels.Add(new KeyValuePair<string, Cairo.Color>("Download", new Cairo.Color (0.2, 1.0, 0.2, 0.7)));
            _labels.Add(new KeyValuePair<string, Cairo.Color>("Upload", new Cairo.Color (1.0, 0.2, 0.2, 0.7)));

            DeviceHandler.DeviceOpened += delegate(AtDevice.AtDevice device)
            {
                device.GetInterface<IHuaweiInterface>().FlowUpdated += FlowUpdated;
            };
        }

        void FlowUpdated(int uptime, int dlSpeed, int ulSpeed, long dlBytes, long ulBytes)
        {
            lock(_data)
            {
                _data[0].Data.Insert(0, dlSpeed/1024);
                _data[1].Data.Insert(0, ulSpeed/1024);
            }
            this.drawingarea.QueueDraw();
        }

        protected virtual void OnStatisticsAreaExposeEvent (object o, Gtk.ExposeEventArgs args)
        {
            using(Cairo.Context ctx = Gdk.Graphics.CreateDrawable(this.drawingarea.GdkWindow))
            lock(_data)
            {
                _drawer.DrawGraph(ctx, drawingarea.GdkWindow.GetSize(), _data, _labels);
            }
        }
    }
}
